package x.trident.core.global;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import x.trident.core.util.DateUtils;
import x.trident.core.constants.Constant;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * @author zza
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Resource
    private HttpServletRequest request;

//    @Resource
//    private AccessTokenStore accessTokenStore;
//
//    @Resource
//    private TokenRequestServices tokenRequestServices;

    @Override
    public void insertFill(MetaObject metaObject) {
        if (Objects.nonNull(RequestContextHolder.getRequestAttributes())) {
            Object enableFlag = this.getFieldValByName("enableFlag", metaObject);
            LocalDateTime now = DateUtils.parseDateTime(DateUtils.formatDateTime(LocalDateTime.now()));
            this.setFieldValByName("enableFlag", Objects.isNull(enableFlag) ? Constant.ENABLE_TRUE : enableFlag, metaObject);
            this.setFieldValByName("deleteFlag", Constant.DELETE_FALSE, metaObject);
            this.setFieldValByName("createTime", now, metaObject);
            this.setFieldValByName("updateTime", now, metaObject);


            this.setFieldValByName("createUserId", 1658001055903371265L, metaObject);
            this.setFieldValByName("createUserName", "admin", metaObject);
            this.setFieldValByName("updateUserId", 1658001055903371265L, metaObject);
            this.setFieldValByName("updateUserName", "admin", metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LocalDateTime now = DateUtils.parseDateTime(DateUtils.formatDateTime(LocalDateTime.now()));
        this.setFieldValByName("updateUserId", 1658001055903371265L, metaObject);
        this.setFieldValByName("updateUserName", "admin", metaObject);
        this.setFieldValByName("updateTime", now, metaObject);
    }


//    @Override
//    public void insertFill(MetaObject metaObject) {
//        if (Objects.nonNull(RequestContextHolder.getRequestAttributes())) {
//            String tokenValue = tokenRequestServices.getAuthorizationByRequest(request);
//            if (StringUtils.isNotBlank(tokenValue)) {
//                MybatisPlusMetaBO mybatisPlusMetaBO = accessTokenStore.loadMybatisPlusMetaObject(tokenValue);
//                if (!Objects.isNull(mybatisPlusMetaBO)) {
//                    this.setFieldValByName("createUserId", mybatisPlusMetaBO.getUserId(), metaObject);
//                    this.setFieldValByName("createUserName", mybatisPlusMetaBO.getName(), metaObject);
//                    this.setFieldValByName("updateUserId", mybatisPlusMetaBO.getUserId(), metaObject);
//                    this.setFieldValByName("updateUserName", mybatisPlusMetaBO.getName(), metaObject);
//                }
//            }
//        }
//        Object id = this.getFieldValByName("id", metaObject);
//        Object enableFlag = this.getFieldValByName("enableFlag", metaObject);
//        LocalDateTime now = DateUtils.parseDateTime(DateUtils.formatDateTime(LocalDateTime.now()));
//        //this.setFieldValByName("id", Objects.isNull(id) ? NumberUtils.generator() : id, metaObject);
//        this.setFieldValByName("enableFlag", Objects.isNull(enableFlag) ? Constant.ENABLE_TRUE : enableFlag, metaObject);
//        this.setFieldValByName("deleteFlag", Constant.DELETE_FALSE, metaObject);
//        this.setFieldValByName("createTime", now, metaObject);
//        this.setFieldValByName("updateTime", now, metaObject);
//    }
//
//    @Override
//    public void updateFill(MetaObject metaObject) {
//        if (Objects.nonNull(RequestContextHolder.getRequestAttributes())){
//            String tokenValue = tokenRequestServices.getAuthorizationByRequest(request);
//            if (StringUtils.isNotBlank(tokenValue)) {
//                MybatisPlusMetaBO mybatisPlusMetaBO = accessTokenStore.loadMybatisPlusMetaObject(tokenValue);
//                if (!Objects.isNull(mybatisPlusMetaBO)) {
//                    this.setFieldValByName("updateUserId", mybatisPlusMetaBO.getUserId(), metaObject);
//                    this.setFieldValByName("updateUserName", mybatisPlusMetaBO.getName(), metaObject);
//                }
//            }
//        }
//        LocalDateTime now = DateUtils.parseDateTime(DateUtils.formatDateTime(LocalDateTime.now()));
//        this.setFieldValByName("updateTime", now, metaObject);
//    }

}